/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.projectdemo.web;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.webbase.common.config.Global;
import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.common.web.BaseController;
import com.foreveross.webbase.projectdemo.entity.Projectapply;
import com.foreveross.webbase.projectdemo.entity.Serverinformation;
import com.foreveross.webbase.projectdemo.entity.Servertype;
import com.foreveross.webbase.projectdemo.service.ProjectapplyService;
import com.foreveross.webbase.projectdemo.service.ServerinformationService;
import com.foreveross.webbase.projectdemo.service.ServertypeService;
import com.foreveross.webbase.system.sys.utils.UserUtils;

/**
 * 项目管理Controller
 * @author yangwanli
 * @version 2017-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/projectdemo/projectapply")
public class ProjectapplyController extends BaseController {

	@Autowired
	private ProjectapplyService projectapplyService;
	@Autowired
	private ServertypeService servertypeService;
	@Autowired
	private ServerinformationService serverinformationService;
	
	
	@RequiresPermissions("projectdemo:projectapply:view")
	@RequestMapping(value = {"list", ""})
	public String list(Projectapply projectapply) {
		//UserUtils.getUser.getId()获取用户id
		projectapply.setUserid(UserUtils.getUser().getId());
		Page<Projectapply> page = projectapplyService.findPage(new Page<Projectapply>(request(), response()), projectapply); 
		attr("page", page);
		attr("aaa",projectapply);
		return "projectdemo/projectapplyList";
	}

	@RequiresPermissions("projectdemo:projectapply:view")
	@RequestMapping("form")
	public String form(String id) {
		if(StringUtils.isNotEmpty(id)) {
			Projectapply projectapply=projectapplyService.get(id);
			attr("projectapply", projectapply);
		} else {
			attr("projectapply", new Projectapply());
		}
		//获取所有服务器类型
		List<Servertype> servertypes = servertypeService.findList(new Servertype());
		attr("alltype",servertypes);
		return "projectdemo/projectapplyForm";
	}

	@RequiresPermissions("projectdemo:projectapply:view,projec")
	@RequestMapping("formId")
	public String formId(String id) {
		if(StringUtils.isNotEmpty(id)) {
			Projectapply projectapply=projectapplyService.get(id);
			attr("projectapply", projectapply);
		}
		return "projectdemo/projectapplyInf";
	}

	
	@RequiresPermissions("projectdemo:projectapply:edit")
	@RequestMapping("save")
	public String save(Projectapply projectapply, RedirectAttributes redirectAttributes) {
		if (!beanValidator(redirectAttributes, projectapply)){
		
			return form(projectapply.getId());
		}
		projectapplyService.save(projectapply);
		//刪除记录
		serverinformationService.deleteId(projectapply.getId());
		//在审批表里添加新的纪录
		serverinformationService.save(new Serverinformation(null,projectapply.getId()));
		addMessage(redirectAttributes, "保存项目成功");
		return "redirect:"+Global.getAdminPath()+"/projectdemo/projectapply/?repage";
	}
	
	@RequiresPermissions("projectdemo:projectapply:edit")
	@RequestMapping("delete")
	public String delete(@RequestParam String id,RedirectAttributes redirectAttributes) {
		projectapplyService.delete(id);
		//同步到审批表删除信息
		serverinformationService.deleteId(id);
		addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/projectdemo/projectapply/?repage";
	}

}